package cn.micai.base.concurrent;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhaoxinguo on 2018/5/14.
 * @email sxdtzhaoxinguo@163.com
 * @qq 827358369
 * @phone 18611966723
 * @description 不公平重入锁
 */
public class ReentrantLockUnfairBeanchmarkDemo implements Counter {
    private volatile long count = 0;

    private Lock lock;

    public ReentrantLockUnfairBeanchmarkDemo() {
        // 使用非公平锁，true就是公平锁
        lock = new ReentrantLock(false);
    }

    @Override
    public long getValue() {
        return count;
    }

    @Override
    public void increment() {
        lock.lock();
        try {
            count++;
        } finally {
            lock.unlock();
        }
    }
}
